/*
 * File ChatSystem.java 
 */
package cs.main;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import cs.controller.Controller;
import cs.gui.ChatGui;
import cs.model.Model;
import cs.ni.ChatNi;

/**
 * The chatSystem class implements Model, GUI and ChatNi classes.<br>
 * Then the controller is constructed with the three precedent classes
 * in parameter to create corrects links for the MVC pattern. <p>
 * Package  cs.main
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
public class ChatSystem{
	/**
	 * udp listening port for incoming messages 
	 */
	private  static int udpListeningPort ;

	/**
	 * tcp listening port for incoming connections
	 */
	private  static int tcpListeningPort  ;

	/**
	 * Port choosen for the group (TCP & UDP)
	 */
	private final static int PORT =  3000 ;

	/**
	 * for logs
	 */
	public static Logger logger;
	/**
	 * Launch the chatSystemApplication
	 * @param args used for changing the port : 
	 * <li>one argument : the same port for TCP & UDP communications</li>
	 * <li>two arguments : the first for UDP and the second for TCP</li>
	 * <li> Warning : the port written in argument must be valid (no test )</li>
	 */
	public static void main(String[] args) {

		//Port configuration
		if (args.length == 1) {
			//one argument -> UDP = TCP 
			System.out.println("UDP & TCP port ="+args[0]);
			udpListeningPort = Integer.parseInt(args[0]) ; 
			tcpListeningPort = Integer.parseInt(args[0]) ; 
		} else if (args.length >= 2) {
			// two arguments -> UDP and TCP
			System.out.println("UDP port = "+args[0]+" & TCP port ="+args[1]);
			udpListeningPort = Integer.parseInt(args[0]) ; 
			tcpListeningPort = Integer.parseInt(args[1]) ; 
		} else {
			// no args -> constant value
			System.out.println("UDP & TCP port = "+PORT);
			udpListeningPort = PORT ; 
			tcpListeningPort = PORT ; 			
		}			


		Model model = new Model();
		ChatGui gui = new ChatGui();
		ChatNi ni = new ChatNi(udpListeningPort, tcpListeningPort);
		Controller controller = new Controller(model, gui, ni);
		gui.setController(controller);
		ni.setController(controller);
		logger = Logger.getLogger("chatSystem");

		try {

			FileHandler fh=new FileHandler("%hchatLog.txt");
			fh.setFormatter(new SimpleFormatter());
			logger.setLevel(Level.INFO);
			fh.setLevel(Level.INFO);
			logger.addHandler(fh);
			logger.log(Level.INFO, "Starting the chat System\n\n");
		} catch (SecurityException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}


	}

}